# -*-coding: utf-8 -*-
"""
    @Theme   : Development Data Faker
    @Time    : 2024/4/10 17:36
    @Author  : YamFish
    @Url     : https://www.zhihu.com/people/yamfish
"""
import random

from faker import Faker
from sqlalchemy.exc import IntegrityError

from suzyblog.models import Admin, Category, Post, Comment, Link
from suzyblog.extensions import db

fake = Faker()


def admin_faker():
    admin = Admin(
        username='admin',
        blog_title='Suzy Blog',
        blog_sub_title='To Make Each Day Count!',
        name='Suzy Li',
        about="""The old adage is true. Time indeed is gold.
But with everything happening so fast, how can you make each day worthwhile?""")

    db.session.add(admin)
    db.session.commit()


def categories_faker(count=10):
    category_default = Category(name='Default')
    db.session.add(category_default)

    for _ in range(count):
        category = Category(name=fake.word())
        db.session.add(category)
        try:
            db.session.commit()
        except IntegrityError:
            db.session.rollback()


def posts_faker(count=50):
    for _ in range(count):
        post = Post(
            title=fake.sentence(),
            body=fake.text(2000),
            timestamp=fake.date_time_this_year(),
            category=Category.query.get(random.randint(1, Category.query.count()))
        )
        db.session.add(post)
    db.session.commit()


def comments_faker(count=500):
    for _ in range(count):
        comment = Comment(
            author=fake.name(),
            email=fake.email(),
            site=fake.url(),
            body=fake.text(50),
            reviewed=True,
            timestamp=fake.date_time_this_year(),
            post=Post.query.get(random.randint(1, Post.query.count()))
        )
        db.session.add(comment)
    db.session.commit()

    # comments from admin
    for _ in range(int(count * 0.1)):
        comment = Comment(
            author=fake.name(),
            email=fake.email(),
            site=fake.url(),
            body=fake.text(50),
            timestamp=fake.date_time_this_year(),
            post=Post.query.get(random.randint(1, Post.query.count())),
            from_admin=True,
            reviewed=True
        )
        db.session.add(comment)
    db.session.commit()

    # replies
    for _ in range(int(count * 0.1)):
        comment = Comment(
            author=fake.name(),
            email=fake.email(),
            site=fake.url(),
            body=fake.text(50),
            reviewed=True,
            timestamp=fake.date_time_this_year(),
            post=Post.query.get(random.randint(1, Post.query.count())),
            replied=Comment.query.get(random.randint(1, Comment.query.count()))
        )
        db.session.add(comment)
    db.session.commit()

    # not review
    for _ in range(int(count * 0.1)):
        comment = Comment(
            author='Suzy',
            email=fake.email(),
            site='https://www.zhihu.com/people/yamfish',
            body=fake.text(50),
            reviewed=False,
            timestamp=fake.date_time_this_year(),
            post=Post.query.get(random.randint(1, Post.query.count()))
        )
        db.session.add(comment)
    db.session.commit()


def links_faker():
    for _ in range(5):
        link = Link(name=fake.word(), url=fake.url())
        db.session.add(link)
    db.session.commit()

